package ucommon

import (
	"net/http"
	"strings"

	"github.com/gin-gonic/gin"
)

//FIXME:GIN框架	reader原样输出

func outputReader(r *http.Response, c *gin.Context) {
	reader := r.Body
	contentLength := r.ContentLength
	contentType := r.Header.Get("Content-Type")

	extraHeaders := map[string]string{
		//"Content-Disposition": `attachment; filename="gopher.png"`,
	}

	c.DataFromReader(http.StatusOK, contentLength, contentType, reader, extraHeaders)
}

//FIXME:GIN框架	get请求，reader原样输出

func GinGet(url *string, c *gin.Context) {
	response, err := http.Get(*url)
	if err != nil || response.StatusCode != http.StatusOK {
		c.Status(http.StatusServiceUnavailable)
		return
	}

	outputReader(response, c)
}

//FIXME:GIN框架	POST请求，reader原样输出

func GinPost(url, body *string, c *gin.Context) {
	readerTmp := strings.NewReader(*body)
	response, err := http.Post(*url,
		"application/json; charset=utf-8", readerTmp)
	if err != nil || response.StatusCode != http.StatusOK {
		c.Status(http.StatusServiceUnavailable)
		return
	}

	outputReader(response, c)
}
